package work.SlidingWindow;

import java.util.ArrayList;
import java.util.List;

public class Problem438 {
    public List<Integer> findAnagrams(String s, String p) {
        int[] s_array = new int[26];
        int[] p_array = new int[26];

        for (int i = 0; i < p.length(); i++) {
            int num = p.charAt(i) - 'a';
            p_array[num]++;
        }

        ArrayList<Integer> ans = new ArrayList<>();
        int left = 0;
        for (int right = 0; right < s.length(); right++) {
            int s_num = s.charAt(right) - 'a';
            s_array[s_num]++;
            while (s_array[s_num] > p_array[s_num]) {
                int curLeft = s.charAt(left) - 'a';
                s_array[curLeft]--;
                left++;
            }
            if (right - left + 1 == p.length()) ans.add(left);

        }
        return ans;
    }


}
